﻿using System.Collections.Generic;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    public class MainPanel:Panel
    {
        private Form1 form1;

        private readonly List<SubPanel> subPanels = new List<SubPanel>();
        private readonly List<CtrlLine> ctrlLabels = new List<CtrlLine>();

        public MainPanel(Form1 _form1)
        {
            form1 = _form1;
            BackColor = form1.BackColor;
            ForeColor = form1.ForeColor;
            /*可以考虑在这里给this加个图案？？*/
        }

        //public void AddTitleFile(Inode inode)
        //{
        //    if (subPanels.Count == 0)
        //    {
        //        SubPanel subPanel = new SubPanel(inode, subPanels,this,form1);

        //        this.Controls.Add(subPanel);
        //        subPanels.Add(subPanel);

        //        RefLct();
        //    }
        //    else
        //    {
        //        subPanels[0].AddTitle(inode);
        //        subPanels[0].Focus1 = true;
        //    }
        //}

        //public void AddSubPanel(Inode inode)
        //{
        //    SubPanel subPanel = new SubPanel(inode, subPanels, this, form1);

        //    double propn = subPanels.Count;
        //    propn /= (propn + 1);  //缩小比例

        //    foreach (SubPanel sub in subPanels)
        //    {
        //        sub.WidthPropn *= propn;
        //    }

        //    //新建一个横向的控制大小label
        //    if (subPanels.Count > 0)
        //    {
        //        CtrlLine ctrlLabel = new CtrlLine(this, subPanels[subPanels.Count - 1], subPanel);

        //        this.Controls.Add(ctrlLabel);
        //        ctrlLabels.Add(ctrlLabel);
        //    }

        //    this.Controls.Add(subPanel);
        //    subPanels.Add(subPanel);

        //    RefLct();

            
        //}

        public void RmSubPanel(SubPanel subPanel)
        {
            double propn = 1 - subPanel.WidthPropn;
            

            for (int i1 = 0; i1 < ctrlLabels.Count; i1++)
            {
                CtrlLine ctrlLabel = ctrlLabels[i1];

                if (subPanel.Equals(ctrlLabel.LeftPanel))//如果被删除的面板是一根控制线的左面板
                {
                    if (subPanels[0].Equals(subPanel))//如果是第一个被删了
                    {
                        ctrlLabels.Remove(ctrlLabel);//把他的控制线删除了
                        this.Controls.Remove(ctrlLabel);
                    }
                    else
                    {
                        for (int i = 0; i < subPanels.Count; i++)
                        {
                            if (subPanels[i].Equals(subPanel))  //修改控制线的左侧面板
                            {
                                ctrlLabel.LeftPanel = subPanels[i - 1];
                            }
                        }
                    }
                    break;
                }
            }

            for (int i1 = 0; i1 < ctrlLabels.Count; i1++)  
            {
                CtrlLine ctrlLabel = ctrlLabels[i1];

                if (subPanel.Equals(ctrlLabel.RightPanel))
                {
                    ctrlLabels.Remove(ctrlLabel);//把他的控制线删除了
                    this.Controls.Remove(ctrlLabel);
                    break;
                }
            }

            int cnt = -1;
            for (int i = 0; i < subPanels.Count; i++)
            {
                if (subPanels[i].Equals(subPanel))
                {
                    cnt = i;
                }
            }

            if (cnt != -1)
            {
                if (subPanels.Count > (cnt + 1))
                {//将焦点给后面一个子面板
                    subPanels[cnt + 1].Focus1 = true;
                }
                else if (cnt > 0)
                { //将焦点给前面一个子面板
                    subPanels[cnt - 1].Focus1 = true;
                }
            }

            subPanels.Remove(subPanel);
            this.Controls.Remove(subPanel);  //再删除他的本体

            foreach (SubPanel sub in subPanels)
            {
                sub.WidthPropn /= propn;
            }

            RefLct();
        }

        //建议在以下几种情况调用本函数
        //在窗口大小出现变化的时候
        //在面板大小发生变化的时候
        //在子面板大小发生变化的时候
        //在子面板数量出现变化的时候

        public void RefLct()
        {
            for (int i = 0; i < subPanels.Count; i++)
            {
                subPanels[i].RefreshLocation(i);
            }

            foreach (CtrlLine ctrlLine in ctrlLabels)
            {
                ctrlLine.RefLct();
            }
        }

    }
}